<?php
class IndexAction extends CommonAction{
	public $like;
	public $org;
	
    public function index(){
		//聚焦图片的轮播
		$Img = M('image')->where('Status=1')->order('id desc')->limit(6)->select();
		$this->assign('Img',$Img);
		$content = M('Content')->limit(4)->select();//debug 重要新闻
		$this->assign('content',$content);
		
		//对left.html模板的输出 全部已换成了widget
		
		// /* 对中间区域的输出 */
		
		//动态快报
		$news = M('Content')->where('ColumnId=2')->order('id desc')->limit(9)->select();
		$this->assign('news',$news);
		
		//专家寄语
		$blessings = M('Content')->where('ColumnId=12')->order('id desc')->limit(9)->select();
		$this->assign('blessings',$blessings);
		
		//大赛文件下载
		$downfile = M('files')->where("TrueName='管理员'")->limit(9)->select();//debug 假设后台管理员的Id=1
		$this->assign('downfile',$downfile);
		
		//大赛顾问列表
		Load('extend');//加载截取函数
		$Model = new Model();
		$expert = $Model->query("select e.Avatar,e.EnterPrise,e.Job,e.Name,c.Id,c.Title,c.Description,c.RedirectUrl from expert e,content c where e.ContentId=c.Id and e.IsGuest=0 order by e.ListId asc limit 6");
		$this->assign('expert',$expert);
		$this->assign('expert2',$expert);
		
		//嘉宾列表
		$Model = new Model();
		$guest = $Model->query("select e.Avatar,e.EnterPrise,e.Job,e.Name,c.RedirectUrl,c.Id from expert e,content c where e.ContentId=c.Id and e.IsGuest=1 order by e.ListId asc limit 6");
		$this->assign('guest',$guest);
		
		//大赛协办单位
		$company = M('Enterprise')->limit(10)->order('id desc')->select();
		$this->assign('company',$company);
		
		
		//团队展示
		$team =M()->query("select t.Id,t.Name,t.Logo,t.SchoolName,ti.TrueName,p.Name as pname from project p,team t,teaminstructors ti where ti.TeamId = t.id and t.id = p.TeamId limit 6 ");
		$this->assign('team',$team);
		
		
		
		//广告展示
		$ads = M('Ads')->order('Id desc')->limit(1)->find();
		$this->assign('ads',$ads);
		// 媒体聚焦
		$media = M('mediaatt')->limit(12)->Select();
		$this->assign('media',$media);

		$this->display();
   }
	//选择登录角色
	public function run(){
		$this->display();
	}
	//分帧导入cms的haier.html对应的方法
	public function haier(){
		$this->display();
	}
	/*
		功能:找回密码
		描述：通过 函数设定PHPMelail 设置传人 用户名、密码
		返回值 无
	*/ 
	public function getPwd(){
		$this->display();
	}
	//执行密码找回邮件的发送
	Public function Melail(){
		//获取被找回密码的账号信息
		$mail=$this->_post('Email') ? $this->_post('Email') : $this->_get('Email');
		$UserName=$this->_post('UserName') ? $this->_post('UserName') : $this->_get('UserName');
		$model=new Model();
		$data=$model->query('SELECT UserId FROM users WHERE UserName="'.$UserName.'" AND Email="'.$mail.'" LIMIT 1');
		if(!$data){
			$this->error('信息错误，您不能修改密码');
			break;
		}
		echo '<pre>';
		//var_dump($data);
		$pwd=rand(100000,999999);
				//定义一个发送邮件的内容
			$message=<<<str
你好！{$username}
	<h2>欢迎使用本网站</h2>
	您现在的密码为:<br/><br/>
	{$pwd}<br /><br />
	www.***.com
str;
					//发送激活账号邮件
		$b =$this->sendMail($mail,"密码找回",$message);
		$pwd=md5($pwd);
		$up=$model->execute('UPDATE `users` SET Password="'.$pwd.'" WHERE UserId='.$data[0]['UserId'].' LIMIT 1');
		
		if($b and $up){
			$this->success('找回密码成功，请登录邮箱找回密码');
		}else{
			echo 'UPDATE `users` SET Password='.$pwd.' WHERE UserId='.$data[0]['UserId'].' LIMIT 1';
			$this->error("邮箱发送失败！请稍后再试！");
		}
	}
	private function sendMail($address,$title,$message){
		require("./Match/Tpl/Include/PHPMailer/class.phpmailer.php");//导入mail类文件
		//创建mail对象
		$mail = new PHPMailer();
		$mail->IsSMTP(); //设置使用SMTP服务器发送
		$mail->Host = C('MAIL_HOST');  //设置126邮箱服务 
		$mail->SMTPAuth = true;     // 设置需要验证
		$mail->Username = C('MAIL_USER');  // 发件人使用邮箱
		$mail->Password = C('MAIL_PWD'); // 设置发件人密码
		$mail->From = C('MAIL_FORM');// 发件人邮箱
		$mail->FromName = C('MAIL_NAME'); //发送者名称
		$mail->AddAddress($address); // 添加发送地址
		$mail->IsHTML(true); //指定支持html格式
		$mail->CharSet="UTF-8";
		$mail->Subject = $title;
		$mail->Body = $message;
		if($mail->Send()){
		   return true;
		}else{
		   return false;
		}
	}
	//文件下载方法
	public function downLoad(){
		$file = M('files')->where('Id='.$this->_get('id'))->find();
		//告诉浏览器字符集
		header('content-type:charset=utf-8');
		
		//告诉浏览器你的文件描述
		header('Content-Disposition:attachment;filename="'.$file[FilePath].'"');
		//告诉浏览器文件的大小
		header('content-length:filesize(UpFile/PublicResource/'.$file[FilePath].')');
		//显示内容
		readfile("UpFile/PublicResource/{$file['FilePath']}");
	}
	
	//竞赛项目
	public function trade(){
		@$page = $this->_get('page');
		$_SESSION['like'] = $this->_post('like');
		$_SESSION['org'] = $this->_post('org');
		if(!intval($page))
			$page=1;
		$trade = new TradeModel($page,$this->like,$this->org);
		$this->assign('enterprise',$trade->enterprise);		//企业列表
		$this->display();
	}
	public function gettradelist(){
		@$page = $this->_get('page');
		if(!intval($page))
			$page=1;
		echo $this->like;
		$trade = new TradeModel($page,$_SESSION['like'],$_SESSION['org']);
		$this->assign('pagenum',$trade->pagenum);		//总页数
		$this->assign('page',$trade->page);				//当前页
		$this->assign('bussinessproblem',$trade->bplist);//竞赛项目列表
		$this->display();
	}
	//大赛Q&A
	public function QA(){
		$message = M('mb_message');
		$p = $this->_get('p');
		if(!intval($p))
			$p = 1;
		$messagelist = $message->where('IsApproved=1')->order('LeaveDate desc')->page($p.',5')->select();
		$this->assign('list',$messagelist);// 赋值数据集
		//$messagelist = $message->query("select Title,Message,LeaveDate,TrueName,LeaveUser from __TABLE__ where IsApproved=1");
		$this->assign('message',$messagelist);
		//加入分页类
		import("ORG.Util.Page");// 导入分页类
		$count = $message->where("IsApproved=1")->count();
		$Page = new Page($count,5);
		$show = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		//高校排行榜数据
			//优秀组织
		$contab_1 = M('Team')->query("select distinct SchoolName from __TABLE__ order by Score desc limit 15");
		$this->assign('contab_1',$contab_1);
			//团队数量
		$contab_2 = M('Team')->query("select SchoolName from __TABLE__ group by SchoolId order by count(Id) desc limit 15");
		$this->assign('contab_2',$contab_2);
			//提交方案
		$contab_3 = M()->query("select t.SchoolName,count(t.SchoolName) as num from team t,project p where t.Id=p.TeamId group by t.SchoolName order by num desc limit 15");
		$this->assign('contab_3',$contab_3);	
		$this->display();
	}
	//添加留言信息
	public function messageCom(){
		if($_SESSION['verify']!=md5($this->_post('txtCode'))){
			$this->error('验证码错误');
		}
		if(!empty($_POST['UserId'])){
			$form['LeaveUser'] = $this->_post('UserId');
			$form['TrueName'] = $this->_post('TrueName');
			$form['Title'] = $this->_post('Title');
			$form['Message'] = $this->_post('Message');
			$form['LeaveDate'] = date('Y-m-d H:i:s');
			$form['IsApproved'] = '0';
			$form['GoTop'] = '0';
			if(!empty($_POST['Title']) && !empty($_POST['Title'])){
				$message = M('mb_message');
				$res = $message->data($form)->add();
			}else{
				$this->error('请写入标题和内容后再提交');
			}
			if($res){
				$this->success('留言成功，请等待回复...');
			}else{
				$this->error('留言失败');
			}
		}else{
			$this->error('登录后才能留言，请登录','login');
		}
	}
}